/*
 * EasyBounceMainView.cpp
 *
 *  Created on: 2009-3-14
 *      Author: Wayne
 */

// INCLUDE FILES
#include <coemain.h>
#include <viewcli.h>
#include <aknviewappui.h>
#include <aknmessagequerydialog.h>
#include <aknnotewrappers.h>
#include <stringloader.h>

#include <EasyBounce.rsg>

#include "EasyBounceMainView.h"
#include "EasyBounceMainContainer.h"
#include "EasyBounce.hrh"

_LIT( KTextEasy, "Easy" );
_LIT( KTextNormal, "Normal" );
_LIT( KTextHard, "Hard" );

const int KDefaultArraySegment = 4;

// ============================ MEMBER FUNCTIONS ===============================

// ---------------------------------------------------------
// CFileFavorMainView::ConstructL(const TRect& aRect)
// EPOC two-phased constructor
// ---------------------------------------------------------
//
void CEasyBounceMainView::ConstructL()
    {
    BaseConstructL( R_EB_MAINVIEW );
    }

// ---------------------------------------------------------
// ---------------------------------------------------------
//
CEasyBounceMainView::CEasyBounceMainView()
	{
	//no implementation needed
	}

// ---------------------------------------------------------
// CEasyBounceMainView::~CEasyBounceMainView()
// Default destructor
// ---------------------------------------------------------
//
CEasyBounceMainView::~CEasyBounceMainView()
    {
    if ( iContainer )
        {
        AppUi()->RemoveFromViewStack( *this, iContainer );
        }

    delete iContainer;
    iContainer=NULL;
    }

// ---------------------------------------------------------
// TUid CEasyBounceMainView::Id()
// This returns the view ID
// ---------------------------------------------------------
//
TUid CEasyBounceMainView::Id() const
    {
    return KMainViewId;
    }

// ---------------------------------------------------------
// CEasyBounceMainView::HandleCommandL(TInt aCommand)
// Here we handle commands for this view. 
// Each view has their own HandleCommandL()
// ---------------------------------------------------------
//
void CEasyBounceMainView::HandleCommandL(TInt aCommand)
    {
    switch ( aCommand )
        {
        default:
            {
            AppUi()->HandleCommandL( aCommand );
            break;
            }
        }
    }

// ---------------------------------------------------------
// CEasyBounceMainView::HandleClientRectChange()
// ---------------------------------------------------------
//
void CEasyBounceMainView::HandleClientRectChange()
    {
    if ( iContainer )
        {
        iContainer->SetRect( ClientRect() );
        }
    }

// ---------------------------------------------------------
// CEasyBounceMainView::DoActivateL(...)
// This is called when a view needs to be activated. 
// This creates container with its controls.
// It also receives messages sent to the view.
// aPrevViewId is passed s.t. we can return to the previous view.
// aCustomMessageId is an ID for the message.
// aCustomMessage is the actual message.
// ---------------------------------------------------------
//
void CEasyBounceMainView::DoActivateL(
   const TVwsViewId& /*aPrevViewId*/,TUid /*aCustomMessageId*/,
   const TDesC8& /*aCustomMessage*/)
    {
    if (!iContainer)
        {
        iContainer = new (ELeave) CEasyBounceMainContainer;
        iContainer->SetMopParent(this);
        iContainer->ConstructL( ClientRect() );
        AppUi()->AddToStackL( *this, iContainer );
        }

    // Message handling would take place here.
   }

// ---------------------------------------------------------
// CEasyBounceMainView::HandleCommandL(TInt aCommand)
// This is called when a view needs to be deactivated. 
// This destroys container with its controls.
// ---------------------------------------------------------
//
void CEasyBounceMainView::DoDeactivate()
    {
    if ( iContainer )
    	{
    	AppUi()->RemoveFromViewStack( *this, iContainer );
    	}
   
    delete iContainer;
   	iContainer = NULL;
    }

// ---------------------------------------------------------
// CEasyBounceMainView::CmdNewGameL()
// This is called when a view needs to be deactivated. 
// This destroys container with its controls.
// ---------------------------------------------------------
//
void CEasyBounceMainView::CmdNewGameL()
	{
	TInt selectedLevel( 3 );

	CDesCArrayFlat* array = new (ELeave) CDesCArrayFlat( KDefaultArraySegment );
	CleanupStack::PushL(array);

	array->AppendL( KTextEasy );
	array->AppendL( KTextNormal );
	array->AppendL( KTextHard );

	CAknListQueryDialog* dlg = new(ELeave) CAknListQueryDialog( &selectedLevel );
	dlg->PrepareLC( R_EB_LEVEL_DIALOG );
	dlg->SetItemTextArray(array);
	dlg->SetOwnershipType(ELbmDoesNotOwnItemArray);
	dlg->RunLD();
	
	if ( selectedLevel < 3 )
		{
		// Now passing the level parameter to view
		// It's be processed by Engine class
		// Or just let GameView pass it to Engine
		TBuf8<10> level;
		level.Copy( array->MdcaPoint( selectedLevel ) );
		
   	    AppUi()->ActivateLocalViewL( 
   	       	    TUid::Uid( EGameViewId ),
   	       	    TUid::Uid( 10 ),
   	       	    level
   	       	    );
		}

	CleanupStack::PopAndDestroy(); //array
	}

// ---------------------------------------------------------
// CEasyBounceMainView::CmdShowRankL()
// This is called when the application needs to be switched from mainview to rankview. 
// ---------------------------------------------------------
//
void CEasyBounceMainView::CmdShowRankL()
	{
	AppUi()->ActivateLocalViewL( TUid::Uid( ERankViewId ) );
	}


// End of File

